package com.gouaixin.bee.beeframework2.model;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.bx.pay.BXPay;
import com.bx.pay.backinf.PayCallback;
import com.google.gson.Gson;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.activity.PayWangyinActivity;
import com.gouaixin.bee.beeframework2.bean.Pay;
import com.gouaixin.bee.beeframework2.extra.alipay.PayDemoActivity;
//import com.gouaixin.bee.beeframework2.extra.wxpay.simcpux.wxapi.WXEntryActivity;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/4/17.
 */
public class PayModel {

    //支付方式的选择
    public static void success(final Context context, final Pay pay) {
        View mView = LayoutInflater.from(context).inflate(R.layout.dialog_pay, null);
        AbDialogUtil.showDialog(mView);

        TextView pay_phone = (TextView) mView.findViewById(R.id.pay_phone);
        TextView pay_wy = (TextView) mView.findViewById(R.id.pay_wy);
        TextView pay_zfb = (TextView) mView.findViewById(R.id.pay_zfb);
        TextView pay_wx = (TextView) mView.findViewById(R.id.pay_wx);

        pay_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.removeDialog(context);
                initWeiPai(context);

            }
        });

        pay_wy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.removeDialog(context);
                initWangying(context,pay);

            }
        });

        pay_zfb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.removeDialog(context);
                initZhifubao(context,pay);

            }
        });

        pay_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.removeDialog(context);
                initWeixin(context);

            }
        });

    }





    //支付方式的选择
    public static void pay(final Context context, final Pay pay) {
        View mView = LayoutInflater.from(context).inflate(R.layout.dialog_pay, null);
        AbDialogUtil.showDialog(mView);

        TextView pay_phone = (TextView) mView.findViewById(R.id.pay_phone);
        TextView pay_wy = (TextView) mView.findViewById(R.id.pay_wy);
        TextView pay_zfb = (TextView) mView.findViewById(R.id.pay_zfb);
        TextView pay_wx = (TextView) mView.findViewById(R.id.pay_wx);

        pay_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.removeDialog(context);
                initWeiPai(context);

            }
        });

        pay_wy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.removeDialog(context);
                initWangying(context,pay);

            }
        });

        pay_zfb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.removeDialog(context);
                initZhifubao(context,pay);

            }
        });

        pay_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbDialogUtil.removeDialog(context);
                initWeixin(context);

            }
        });

    }

    /**
     * 微信支付
     * @param context
     */
    public static void initWeixin(Context context) {
        AbToastUtil.showToast(context,"微信支付功能正在开发中");
//        Intent it=new Intent(context, PayActivity.class);
//        context.startActivity(it);


//        String url=
        //{
//        "account": "cf_qutian",
//        "password": "123456",
//        "mobile": "13419527681",
//        "content":"消息测试"
//        }
//        SMS bean=new SMS();
//        bean.setContent("cf_qutian");
//        bean.setAccount("cf_qutian");
//        bean.setMobile("13419527681");
//        bean.setPassword("123456");
//        String json=new Gson().toJson(bean);
//
//        mLog.e("测试postJson：请求="+json);
//
//        String url="";
//
//        byte[] result= HttpUtil.httpPost(url, json);
//
//        String res = new String(result);
//
//        mLog.e("测试postJson：响应="+res);

    }

    /***
     * 支付包支付
     * @param context
     */
    public static void initZhifubao(Context context, Pay pay) {

        Intent it=new Intent(context, PayDemoActivity.class);
        it.putExtra("pay",new Gson().toJson(pay));
        context.startActivity(it);
    }

    //网银+支付
    public static void initWangying(Context context,Pay bean) {




//        WebView webView= (WebView) LayoutInflater.from(context).inflate(R.layout.pay_web,null);
//
//        AlertDialog.Builder builder;
//        final AlertDialog alertDialog;
//        builder = new AlertDialog.Builder(context,R.style.my_dialog);
//        alertDialog = builder.create();
//        alertDialog.setView(webView, 0, 0, 0, 0);
//        alertDialog.show();
//
//        //发现分类的网页视图
//
//        WebSettings webSetting = webView.getSettings();
//        webSetting.setJavaScriptEnabled(true);
//
//        webSetting.setUseWideViewPort(true);
//
//        webView.requestFocus();
//        webView.addJavascriptInterface(context,"jiekou");

//        webView.loadUrl("http://jiekou.hydgyx.com/mclient-php/wangyin/wepay/join/demo/tpl/payIndex.php");
//        webView.loadUrl("http://jiekou.hydgyx.com/mclient-php/wangyin/wepay/join/demo/tpl/payIndex.php");

        Log.e("test","网银+准备上传的pay对象："+new Gson().toJson(bean));

        //拼接url
        String url="";
//        String weburl="http://jiekou.hydgyx.com/mclient-php/mclient-php/wangyin/wepay/join/demo/tpl/payIndex.php";
        String weburl="http://jiekou.hydgyx.com/wangyin/wangyin/wepay/join/demo/tpl/payIndex.php";
        String cid=bean.getCid();
        String tradeName=bean.getTradeName();
        String amount=bean.getAmount()+"00";

        String md5= PreferencesUtils.getString(context,"md5");

        url=weburl+"?cid="+cid+"&tradeName="+tradeName+"&amount="+amount+"&cipher="+md5;
        mLog.e("url=" + url);


        Intent it=new Intent(context, PayWangyinActivity.class);
        it.putExtra("pay",new Gson().toJson(bean));
        it.putExtra("url",url);
        context.startActivity(it);


//        //设置Web视图
//
//        webView.loadUrl(url);
//
//        webView.setWebViewClient(new HelloWebViewClient());

    }



    //神州支付（手机充值卡）
    public static void initShenzhou(Context context,Pay bean) {
        Log.e("test","神舟付准备上传的pay对象："+new Gson().toJson(bean));

        //拼接url
        String url="";
//        String weburl="http://jiekou.hydgyx.com/mclient-php/mclient-php/wangyin/wepay/join/demo/tpl/payIndex.php";
        String weburl="http://jiekou.hydgyx.com/shenzhoufu/send.php";
        String cid=bean.getCid();
        String tradeName=bean.getTradeName();
        String amount=bean.getAmount()+"00";

        String md5= PreferencesUtils.getString(context,"md5");

        url=weburl+"?cid="+cid+"&tradeName="+tradeName+"&amount="+amount+"&cipher="+md5;
        mLog.e("url=" + url);

        Intent it=new Intent(context, PayWangyinActivity.class);
        it.putExtra("url",url);
        it.putExtra("pay",new Gson().toJson(bean));
        context.startActivity(it);

    }


    //微派支付
    public static void initWeiPai(final Context context) {
        String payCode = "0001";

        BXPay bxpay = new BXPay(context);
        bxpay.init(context);
        Map<String, String> devPrivate = new HashMap<String, String>();
        devPrivate.put("开发者要传的KEY值", "开发者要传的VALUE值");
        bxpay.setDevPrivate(devPrivate);//setDevPrivate方式是非必选
        bxpay.pay(payCode, new PayCallback() {

            @Override
            public void pay(Map resultInfo) {
                mLog.e("weipai:resultInfo=" + resultInfo.toString());

                String result = (String) resultInfo.get("result");//描述：  result返回微派支付结果 目前有如下几个状态1、success:成功支付，表示支付成功。2、pass:表示在付费周期里已经成功付费，已经是付费用户。3、pause:表示计费点暂时停止。4、error:本地联网失败、获取不到支付参数等情况。5、fail:支付失败。6、cancel:表示用户取消支付。
                String payType = (String) resultInfo.get("payType");//描述：支付方式类型   目前有如下几个类型   1、	wpay:微派短信支付 。2、	alipay:支付宝支付。3、	czk:充值卡支付。4、	dk:点卡支付。5、	balance：余额支付。6、	uppay:银联支付。
                String payCode = (String) resultInfo.get("payCode");//描述：计费点编号
                String price = (String) resultInfo.get("price");//描述：价格
                String logCode = (String) resultInfo.get("logCode");//订单编号
                String showMsg = (String) resultInfo.get("showMsg");//支付结果详细描述提示
                //以下是非必需調用
                new AlertDialog.Builder(context).setTitle("支付结果返回：")
                        .setMessage(Html.fromHtml("支付结果：" + result + "<br>" +
                                "支付类型：" + payType + "<br>" +
                                "计费点：" + payCode + "<br>" +
                                "计费价格：" + price + "<br>" +
                                "订单编号：" + logCode + "<br>" +
                                "支付结果描述：" + showMsg + "<br>"))
                        .setPositiveButton("确定", null).show();

//
            }
        });

    }




}
